﻿#include <stdio.h>
#include <stdlib.h>

// rtp库依赖socket,必须再rtp库引入之前添加，否则会出各种错误
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")

// rtp库引入
#include "jrtplib3/rtpsession.h"
#include "jrtplib3/rtpudpv4transmitter.h"
#include "jrtplib3/rtpipv4address.h"
#include "jrtplib3/rtpsessionparams.h"
#include "jrtplib3/rtperrors.h"
//#pragma comment(lib, "jrtplib.lib")

using namespace jrtplib;

#pragma warning(disable:4996)

int main(void)
{
	RTPSession  rtpSession;
	RTPSessionParams rtpSessionParams;
	RTPUDPv4TransmissionParams rtpUdpv4Transmissionparams;

	char buf[1024] = { 0x00 };
	char ip[16] = { 0x00 };
	int port = 0;
	int ret = 0;

	// 容易忽略，因为自写代码中没有调用socket，rtp有调用但是没有初始化
	WSADATA dat;
	WSAStartup(MAKEWORD(2, 2), &dat);

	printf("This is sender!!!\n");

	printf("Input destination ip:");
	scanf("%s", ip);
	printf("Input destination port:");
	scanf("%d", &port);
	printf("Destination %s:%d\n", ip, port);

	rtpSessionParams.SetOwnTimestampUnit(1.0 / 1);
	rtpSessionParams.SetUsePollThread(true);
	rtpSessionParams.SetAcceptOwnPackets(false);
	ret = rtpSession.Create(rtpSessionParams, &rtpUdpv4Transmissionparams);
	if (ret < 0)
	{
		printf("Failed to RtpSession::Create, ret=%d\n", ret);
	}

	RTPIPv4Address addr(ntohl(inet_addr(ip)), port);
	rtpSession.AddDestination(addr);

	while (true)
	{
		printf("Input message:");
		scanf("%s", buf);
		if (strcmp(buf, "exit") == 0)
		{
			break;
		}
		ret = rtpSession.SendPacket((void*)buf, strlen(buf), 0, false, 1);
		if (ret < 0)
		{
			printf("Failed to RtpSession::SendPacket, ret=%d\n", ret);
			continue;
		}
		else {
			printf("Succeed to RtpSession::SendPacket!!!\n");
		}
		RTPTime::Wait(RTPTime(0, 100));
	}
	return 0;
}